import {queryStatements, queryStatetemntDetail, createStatement, approvalStatement, payStatement, getStatement, queryPayStatement, updateStatementStatus, queryApprovalStatement, generateStatement} from '../../services/settlement/settlement';
import {parse} from 'qs';
import { message } from 'antd';
import Constants from '../../utils/Constants';
const R=require("ramda");

export default {
    namespace: 'settlement',
    state: {
        page: {},
        list: [],
        feeList: [],
        dataSource: {}
    },
    reducers: {
        /**
         * 刷新state
         *
         * @param state
         */
        refreshState(state, { payload }){
            return {...state, ...payload };
        }
    },
    effects: {
        /**
         * 多条件分页查询结算单列表
         *
         * @param call
         * @param put
         * @param select
         */
            *queryStatements({ payload }, { call, put, select }) {
            const res = yield call(queryStatements, { ...payload });
            if (res.data.resultCode === "ok") {
                let content = res.data.content;
                yield put({
                    type: 'refreshState',
                    payload: {
                        list: content.list,
                        page: content.page ? content.page : Constants.DEFAULT_PAGE
                    }
                });
            } else {
                message.error(res.data.errMessage);
            }
        },
        /**
         * 按供应商、订单id、费用类型查询订单费用列表
         *
         * @param payload
         * @param call
         * @param put
         * @param select
         */
            *queryStatetemntDetail({ payload }, { call, put, select }) {
            const res = yield call(queryStatetemntDetail, { ...R.omit(['onSuccess', 'onFail'])(payload) });
            if (res.data.resultCode === "ok") {
                let content = res.data.content;
                yield put({
                    type: 'refreshState',
                    payload: {
                        feeList: content
                    }
                });
            } else {
                message.error(res.data.errMessage);
            }
        },
        /**
         * 创建结算单
         *
         * @param call
         * @param put
         * @param select
         */
            *createStatement({ payload }, { call, put, select }) {
            const res = yield call(createStatement, { ...payload });
            if (res.data.resultCode === "ok") {
                message.info("创建成功！");
                if(payload.onSuccess) payload.onSuccess();
            } else {
                message.error(res.data.errMessage);
            }
        },
        /**
         * 结算单审批
         *
         * @param call
         * @param put
         * @param select
         */
            *approvalStatement({ payload }, { call, put, select }) {
            const res = yield call(approvalStatement, { ...payload });
            if (res.data.resultCode === "ok") {
                message.info("审批成功！");
                if(payload.onSuccess) payload.onSuccess();
            } else {
                message.error(res.data.errMessage);
            }
        },
        /**
         * 结算单付款
         *
         * @param call
         * @param put
         * @param select
         */
            *payStatement({ payload }, { call, put, select }) {
            const res = yield call(payStatement, { ...payload });
            if (res.data.resultCode === "ok") {
                message.info("付款成功！");
                if(payload.onSuccess) payload.onSuccess();
            } else {
                message.error(res.data.errMessage);
            }
        },
        /**
         * 获取结算单信息和结算单明细
         *
         * @param payload
         * @param call
         * @param put
         * @param select
         */
            *getStatement({ payload }, { call, put, select }) {
            const res = yield call(getStatement, { id: payload.id });
            if (res.data.resultCode === "ok") {
                let content = res.data.content;
                yield put({
                    type: 'refreshState',
                    payload: {
                        dataSource: content
                    }
                });
            } else {
                message.error(res.data.errMessage);
            }
        },
        /**
         * 结算单付款信息查询
         *
         * @param payload
         * @param call
         * @param put
         * @param select
         */
            *queryPayStatement({ payload }, { call, put, select }) {
            const res = yield call(queryPayStatement, { id: payload.id });
            if (res.data.resultCode === "ok") {
                let content = res.data.content;
                yield put({
                    type: 'refreshState',
                    payload: {
                        dataSource: content
                    }
                });
            } else {
                message.error(res.data.errMessage);
            }
        },
        /**
         * 更新结算单状态
         *
         * @param call
         * @param put
         * @param select
         */
            *updateStatementStatus({ payload }, { call, put, select }) {
            const res = yield call(updateStatementStatus, {...payload});
            if (res.data.resultCode === "ok") {
                message.info("更新成功！");
                if (payload.onSuccess) payload.onSuccess();
            } else {
                message.error(res.data.errMessage);
            }
        },
        /**
         * 结算单审核信息列表查询
         *
         * @param call
         * @param put
         * @param select
         */
            *queryApprovalStatement({ payload }, { call, put, select }) {
            const res = yield call(queryApprovalStatement, { ...payload });
            if (res.data.resultCode === "ok") {
                let content = res.data.content;
                yield put({
                    type: 'refreshState',
                    payload: {
                        list: content.list,
                        page: content.page
                    }
                });
            } else {
                message.error(res.data.errMessage);
            }
        },
        /**
         * 导出申请书
         * @param payload
         * @param call
         * @param put
         * @param select
         */
            *generateStatement({ payload }, { call, put, select }) {
            const res = yield call(generateStatement, {id: payload.id});
        },

    },
    // subscriptions: {
    //     setup({history, dispatch}) {
    //         return history.listen(({pathname}) => {
    //             dispatch({
    //                 type: 'queryStatements',
    //                 payload: {
    //                     page: Constants.DEFAULT_PAGE
    //                 }
    //             });
    //         });
    //     }
    // }
};
